package edu.neumont.MillerK.Web.Servlet;

import java.io.IOException;

import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.UserTransaction;

import edu.neumont.MillerK.POJOs.Security;
import edu.neumont.MillerK.Web.DBQueries.DBQueries;
import edu.neumont.MillerK.Web.Entities.User;

/**
 * Servlet implementation class FeaturesServlet
 */
@WebServlet("/Features")
public class FeaturesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Resource
	UserTransaction utx;

	@PersistenceUnit
	EntityManagerFactory emf;

	private DBQueries dbQueries;

	private final int panicModeIndex = 0;
	private final int screamerModeIndex = 1;
	private final int wifiIndex = 2;
	private final int bluetoothIndex = 3;
	private final int threeGIndex = 4;
	private final int fourGIndex = 5;

	private char[] features;
	private char[] commands;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		User currentUser = (User) request.getSession().getAttribute(
				"currentUser");
		if (currentUser == null)
			response.sendRedirect("Home");
		else {
			dbQueries = new DBQueries(emf, utx);
			currentUser = dbQueries.getUser(currentUser.getEmailUsername());
			request.getSession().setAttribute("currentUser", currentUser);
			char[] featureArray = currentUser.getFeatures().toCharArray();
			request.setAttribute("featureArray", featureArray);
			request.getRequestDispatcher("/WEB-INF/views/Features.jsp")
					.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("POST");
		dbQueries = new DBQueries(emf, utx);
		User currentUser = (User) request.getSession().getAttribute(
				"currentUser");
		features = currentUser.getFeatures().toCharArray();
		commands = currentUser.getCommands().toCharArray();
		if (request.getParameter("featureName").equals("panicMode")) {
			setCommand(panicModeIndex);
		} else if (request.getParameter("featureName").equals("screamerMode")) {
			setCommand(screamerModeIndex);
		} else if (request.getParameter("featureName").equals("WiFi")) {
			setCommand(wifiIndex);
		} else if (request.getParameter("featureName").equals("bluetooth")) {
			setCommand(bluetoothIndex);
		} else if (request.getParameter("featureName").equals("3G")) {
			setCommand(threeGIndex);
		} else if (request.getParameter("featureName").equals("4G")) {
			setCommand(fourGIndex);
		}
		System.out
				.println("[FeaturesServlet] commands: " + commands.toString());
		dbQueries.updateUserCommands(currentUser, commands);
		doGet(request, response);
	}

	private void setCommand(int index) {
		if (features[index] == '0') {
			commands[index] = '1';
		} else {
			commands[index] = '0';
		}
	}
}
